// import { get as _get } from 'object-path';
// import { message } from 'antd';
// import { history } from 'umi';
import {
    fetchVacationList,
    getColumnlist,
    getModalCompany,
    downTemplate,
    getPopoverList,
    getExportlist,
    updatevocationbalance,
    getModalColumnlist
} from '@/services/attendance/vacationBalance';
import { getDict } from '@/services/myRecruit/talentPool';

export default {
    namespace: 'vacationBalance',
    state: {
        vacationBalanceList: [],
        vacation: [],
        allSelectVacation: [], // 默认全选的leaveCode
        pagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        timeUnitTypeList: [],
        vocationRecordTypeList: [],
        companyList: [],
        modalVacation: [],
        popoverList: [],
        demoTable: [
            {
                userNo: '',
                employeeName: '',
                departmentName: '',
                CommonYear: '15',
                CommonSick: '14',
                SgHospital: '46'
            },
            {
                userNo: '',
                employeeName: '',
                departmentName: '',
                CommonYear: '15',
                CommonSick: '14',
                SgHospital: '46'
            },
            {
                userNo: '',
                employeeName: '',
                departmentName: '',
                CommonYear: '12.5',
                CommonSick: '14',
                SgHospital: '46'
            }
        ]
    },
    effects: {
        // 获取员工假期余额列表
        *fetchVacationList({ payload }, { put, call }) {
            const result = yield call(fetchVacationList, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        vacationBalanceList: result.data.rows,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 根据假期类型显示对应余额
        *getPopoverList({ payload }, { put, call }) {
            const result = yield call(getPopoverList, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        popoverList: result.data
                    }
                });
            }
        },
        // 获取显示列
        *getColumnlist({ payload }, { put, call }) {
            const result = yield call(getColumnlist, payload);
            if (result && result.code === 10000) {
                const arr = [];
                result.data.map(item => {
                    arr.push(item.leaveCode);
                    return '';
                });
                yield put({
                    type: 'save',
                    payload: {
                        vacation: result.data,
                        allSelectVacation: arr
                    }
                });
            }
        },
        // 获取显示列
        *getModalColumnlist({ payload }, { put, call }) {
            const result = yield call(getModalColumnlist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        modalVacation: result.data
                    }
                });
            }
        },

        // 获取modal里的字典
        *getModalDict(_, { call, put, all }) {
            const [timeUnitType, vocationRecordType] = yield all([
                call(getDict, { code: 'balanceTimeUnitType' }),
                call(getDict, { code: 'vocationRecordType' })
            ]);
            if (timeUnitType.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        timeUnitTypeList: timeUnitType.data
                    }
                });
            }
            if (vocationRecordType.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        vocationRecordTypeList: vocationRecordType.data
                    }
                });
            }
        },
        // 获取modal里的公司数据
        *getModalCompany({ payload }, { put, call }) {
            const result = yield call(getModalCompany, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        companyList: result.data
                    }
                });
            }
        },
        // 修改假期余额
        *updatevocationbalance({ payload }, { call }) {
            const result = yield call(updatevocationbalance, payload);
            if (result) {
                return result;
            }
            return null;
        },

        // 下载公司模板
        *downTemplate({ payload }, { call }) {
            const result = yield call(downTemplate, payload);
            return result;
        },
        // 获取modal里的公司数据
        *fetchExportlist({ payload }, { call }) {
            const result = yield call(getExportlist, payload);
            return result;
        }
        // // 批量上传假期余额
        // *uploadExcel({ payload }, { call }) {
        //     const result = yield call(uploadExcel, payload);
        // }
    },
    reducers: {
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        },
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
